/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.Components;

import General.CurrentData;
import com.jme3.math.Vector3f;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.FocusListener;
import java.awt.event.KeyListener;
import java.awt.event.MouseListener;
import java.text.DecimalFormat;
import javax.swing.JPanel;
import javax.swing.event.DocumentListener;

/**
 *
 * @author David
 */
public class Float3Panel extends JPanel
{

    private BTextField xField = new BTextField("Float", "x"),
            yField = new BTextField("Float", "y"),
            zField = new BTextField("Float", "z");
    private Dimension prefDimension = new Dimension(65, 20);
    private boolean manualUserChangeMade = false;
    private DecimalFormat rounder = new DecimalFormat("0.000");

    /**
     *
     * @param vec
     */
    public Float3Panel(Vector3f vec)
    {
        addFieldMouseListener(new InsertVectorMouseListener(this));
        if (vec != null)
        {
            xField.setText("" + rounder.format(vec.getX()).replace(',', '.'));
            yField.setText("" + rounder.format(vec.getY()).replace(',', '.'));
            zField.setText("" + rounder.format(vec.getZ()).replace(',', '.'));
        }
        xField.setPreferredSize(prefDimension);
        yField.setPreferredSize(prefDimension);
        zField.setPreferredSize(prefDimension);
        setLayout(new FlowLayout(FlowLayout.LEFT));
        add(xField);
        add(yField);
        add(zField);
    }

    /**
     *
     * @return
     */
    public Vector3f getVector()
    {
        float x = 0, y = 0, z = 0;
        try
        {
            x = Float.parseFloat(xField.getText());
        } catch (java.lang.NumberFormatException nfe)
        {
            CurrentData.getObserverDialog().printMessage("Error returning Vector3 x value from Float3Panel, returning x = 0");
        }
        try
        {
            y = Float.parseFloat(yField.getText());
        } catch (java.lang.NumberFormatException nfe)
        {
            CurrentData.getObserverDialog().printMessage("Error returning Vector3 y value from Float3Panel, returning y = 0");
        }
        try
        {
            z = Float.parseFloat(zField.getText());
        } catch (java.lang.NumberFormatException nfe)
        {
            CurrentData.getObserverDialog().printMessage("Error returning Vector3 z value from Float3Panel, returning z = 0");
        }
        return new Vector3f(x, y, z);

    }

    /**
     *
     * @return true if one of the textfields is focused
     */
    @Override
    public boolean hasFocus()
    {
        return (xField.hasFocus() || yField.hasFocus() || zField.hasFocus());
    }

    /**
     *
     * @param vec
     */
    public void setVector(Vector3f vec)
    {
        //   System.out.println(vec);
        xField.setText("" + rounder.format(vec.getX()).replace(',', '.'));
        yField.setText("" + rounder.format(vec.getY()).replace(',', '.'));
        zField.setText("" + rounder.format(vec.getZ()).replace(',', '.'));
    }

    /**
     *
     * @param focusListener
     */
    public void addFieldFocusListener(FocusListener focusListener)
    {
        xField.addFocusListener(focusListener);
        yField.addFocusListener(focusListener);
        zField.addFocusListener(focusListener);
    }

    /**
     *
     * @param mouseListener
     */
    public void addFieldMouseListener(MouseListener mouseListener)
    {
        xField.addMouseListener(mouseListener);
        yField.addMouseListener(mouseListener);
        zField.addMouseListener(mouseListener);
    }

    /**
     *
     * @param keyListener
     */
    public void addFieldKeyListener(KeyListener keyListener)
    {
        xField.addKeyListener(keyListener);
        yField.addKeyListener(keyListener);
        zField.addKeyListener(keyListener);
    }

    /**
     *
     * @param documentListener
     */
    public void addFieldDocumentListener(DocumentListener documentListener)
    {
        xField.getDocument().addDocumentListener(documentListener);
        yField.getDocument().addDocumentListener(documentListener);
        zField.getDocument().addDocumentListener(documentListener);
    }

    /**
     *
     * @return
     */
    public BTextField getxField()
    {
        return xField;
    }

    /**
     *
     * @return
     */
    public BTextField getyField()
    {
        return yField;
    }

    /**
     *
     * @return
     */
    public BTextField getzField()
    {
        return zField;
    }

    /**
     *
     * @return
     */
    public boolean isManualUserChangeMade()
    {
        return manualUserChangeMade;
    }

    /**
     *
     * @param manualUserChangeMade
     */
    public void setManualUserChangeMade(boolean manualUserChangeMade)
    {
        this.manualUserChangeMade = manualUserChangeMade;
    }

    public DecimalFormat getRounder()
    {
        return rounder;
    }
}
